﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace 卷书.Utils
{
    public sealed class Settings
    {
        private static string ConfigPath = "Config.xml";
        private static string RootName = "Settings";
        private static XmlDocument XmlDoc;
        private static readonly Settings instance = new Settings();
        public static event EventHandler CurrentProjectChanged;

        private static string _CurrentProject="";
        public static string CurrentProject
        {
            get
            {
                return _CurrentProject;
            }
            set
            {
                _CurrentProject = value;
                CurrentProjectChanged?.Invoke(null, null);
            }
        }


        private static List<string> _RecentProject = new List<string>();
        public static List<string> RecentProject
        {
            get
            {
                return _RecentProject;
            }
            set
            {
                _RecentProject = value;
            }
        }


        private Settings()
        {
            XmlDoc = new XmlDocument();
        }

        public static Settings Instance
        {
            get
            {
                return instance;
            }
        }




        /// <summary>
        /// 每次启动后即初始化
        /// </summary>
        public void Init()
        {
            //初始化配置文件
            if (!File.Exists(ConfigPath))
            {
                XmlNode header = XmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
                XmlDoc.AppendChild(header);
                var xm = XmlDoc.CreateElement(RootName);
                XmlDoc.AppendChild(xm);
                Save();
            }
            else
            {
                XmlDoc.Load(ConfigPath);
            }
            ReadAllSetting();
        }


        private void ReadAllSetting()
        {
            CurrentProject = ReadByName("CurrentProject");
            string recentProject = ReadByName("RecentProject");
            if (recentProject.IndexOf("*") > 0)
            {
                //读取数据
                    RecentProject = recentProject.Split('*').Where(arg => File.Exists(arg)).Take(10).ToList();
            }
            else if (File.Exists(recentProject))
            {
                RecentProject.Add(recentProject);
            }
        }


        public void SaveAllSetting()
        {
            SaveByName("CurrentProject", CurrentProject);
            SaveByName("RecentProject", string.Join("*", RecentProject));
        }

        private void Save()
        {
            try
            {
                XmlDoc.Save(ConfigPath);
            }
            catch (XmlException ex)
            {
                Logger.LogF(ex);
            }
        }

        public string ReadByName(string name)
        {
            XmlNode x1 = XmlDoc.SelectSingleNode($"/Settings/Setting[@Name='{name}']");
            if (x1 != null) return x1.InnerText;
            return "";
        }

        public void SaveByName(string name, string value)
        {
            XmlNode node = XmlDoc.SelectSingleNode($"/Settings/Setting[@Name='{name}']");
            XmlElement rootElement = XmlDoc.DocumentElement;
            if (node == null && rootElement!=null)
            {
                XmlElement XE = XmlDoc.CreateElement("Setting");
                XE.SetAttribute("Name", name);
                XE.InnerText = value;
                rootElement.AppendChild(XE);
            }
            else
            {
                node.InnerText = value;
            }

            Save();
        }





    }
}
